package com.llb666.shop.order.domain.service.center;

import com.llb666.common.core.enums.OrderStatusEnum;
import com.llb666.common.core.exception.AppException;
import com.llb666.common.core.utils.page.PagedGridResult;
import com.llb666.common.core.web.error.CommonError;
import com.llb666.shop.order.api.vo.center.MyOrdersVo;
import com.llb666.shop.order.api.vo.OrderStatusCountsVo;
import com.llb666.shop.order.domain.entity.OrderItems;
import com.llb666.shop.order.domain.entity.OrderStatus;
import com.llb666.shop.order.domain.entity.Orders;
import com.llb666.shop.order.domain.repository.OrderItemsRepository;
import com.llb666.shop.order.domain.repository.OrderStatusRepository;
import com.llb666.shop.order.domain.repository.OrdersRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * 用户中心：我的订单service.
 *
 * @author llb666
 */
@Service
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class MyOrdersService {

    private final OrdersRepository ordersRepository;
    private final OrderItemsRepository orderItemsRepository;
    private final OrderStatusRepository orderStatusRepository;

    /**
     * 获得登录用户的订单状态数概况.
     *
     * @param userId 用户id
     * @return 订单状态数目概况
     */
    public OrderStatusCountsVo getOrderStatusCounts(String userId) {
        // 待付款.
        int waitPayCounts = ordersRepository.getOrderStatusCounts(userId, OrderStatusEnum.WAIT_PAY.type);
        // 待发货.
        int waitDeliverCounts = ordersRepository.getOrderStatusCounts(userId, OrderStatusEnum.WAIT_DELIVER.type);
        // 待收货.
        int waitReceiveCounts = ordersRepository.getOrderStatusCounts(userId, OrderStatusEnum.WAIT_RECEIVE.type);
        // 交易成功,待评论.
        int waitCommentCounts = ordersRepository.getOrderStatusCounts(userId, OrderStatusEnum.SUCCESS.type);
        return new OrderStatusCountsVo(waitPayCounts, waitDeliverCounts, waitReceiveCounts, waitCommentCounts);
    }

    /**
     * 商家发货，更新订单状态为"已发货，待收货"".
     *
     * @param orderId 订单id
     */
    public void updateDeliverOrderStatus(String orderId) {
        ordersRepository.updateDeliverOrderStatus(orderId);
    }

    /**
     * 查询订单列表。
     *
     * @param userId      用户id
     * @param orderStatus 订单状态
     * @param page        页码
     * @param pageSize    每页显示数量
     * @return PagedGridResult
     */
    public PagedGridResult<MyOrdersVo> queryMyOrders(String userId, Integer orderStatus, Integer page, Integer pageSize) {
        return ordersRepository.queryMyOrderByStatus(userId, orderStatus, page, pageSize);
    }

    /**
     * 更新订单状态为"交易成功".
     *
     * @param orderId 订单id
     * @return true/false
     */
    public boolean updateReceiveOrderStatus(String orderId) {
        return ordersRepository.updateReceiveOrderStatus(orderId);
    }

    /**
     * 用户删除订单.
     *
     * @param userId  用户id
     * @param orderId 订单id
     * @return true/false
     */
    public boolean deleteOrder(String userId, String orderId) {
        return ordersRepository.deleteOrder(userId, orderId);
    }

    /**
     * 查询用户的订单动态.
     *
     * @param userId   用户id
     * @param page     页数
     * @param pageSize 每页显示树木
     * @return 订单列表
     */
    public PagedGridResult<OrderStatus> getOrdersTrend(String userId, Integer page, Integer pageSize) {
        return ordersRepository.getMyOrderTrend(userId, page, pageSize);
    }

    /**
     * 查询代办订单.
     *
     * @param orderId 订单id
     * @return 订单列表
     */
    public List<OrderItems> queryPendingComment(String orderId) {
        return orderItemsRepository.queryPendingComment(orderId);
    }

    /**
     * 用于验证用户和订单是否有关联关系，避免非法用户调用.
     */
    public void checkUserOrder(String userId, String orderId) {
        Orders order = ordersRepository.queryMyOrder(userId, orderId);
        if (order == null) {
            throw new AppException(CommonError.NotAuthorized, "订单不存在");
        }
    }

    /**
     * 根据用户和订单id查询订单信息.
     *
     * @param userId  用户id
     * @param orderId 订单id
     * @return 订单
     */
    public Orders details(String userId, String orderId) {
        return ordersRepository.queryMyOrder(userId, orderId);
    }

    /**
     * 更新订单为"已评价".
     *
     * @param orderId 订单id
     */
    public void updateCommentsStatus(String orderId) {
        ordersRepository.updateCommentsStatus(orderId);
    }

    /**
     * 修改订单状态表的留言时间.
     *
     * @param orderId 订单id
     */
    public void updateOrderStatus(String orderId) {
        orderStatusRepository.updateOrderStatus(orderId);
    }
}
